#include <fstream>
#include <iostream>
#include <iterator>
#include <functional>
#include <algorithm>
#include <sstream>

#include <stdlib.h>

#include "generator.h"

Generator::Generator(std::string& fname) {
  std::ifstream inFile(fname.c_str());
  if (!inFile)
    std::cout << "Herota111" << std::endl;

  std::copy(
    std::istream_iterator<std::string>(inFile), 
    std::istream_iterator<std::string>(), 
    std::back_inserter(_candidates));

  for (int i = 1; i <= _candidates.size(); i++)
    _cache.push_back(i);

  _index = 0;

  inFile.close();
}

int Generator::Generate(std::string& out, unsigned ammount) {
  std::ofstream outFile(out.c_str());

  if (!outFile)
    std::cout << "Herota111" << std::endl;

  for (int i = 0; i < ammount; i++) {
    std::random_shuffle(_cache.begin(), _cache.end());
    for (std::vector<int>::iterator iter = _cache.begin(); iter != _cache.end(); iter++)
      outFile << "<" << *iter << ">";
    outFile << " index" << i << std::endl;
  }

  outFile.close();
}

std::pair<std::string, int> Generator::GeneratePermutation() {
  std::stringstream ss;
  std::random_shuffle(_cache.begin(), _cache.end());
  for (std::vector<int>::iterator iter = _cache.begin(); iter != _cache.end(); iter++)
    ss << "<" << *iter << ">";

  return std::pair<std::string, int>(std::string(ss.str()), _index++);
}
